package top.zhimingw.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import top.zhimingw.pojo.Statistics;
import top.zhimingw.redis.StatisticsKey;
import top.zhimingw.service.BaseService;
import top.zhimingw.service.StatisticsService;
import top.zhimingw.utils.IPUtils;
import top.zhimingw.vo.StatisticsCount;
import top.zhimingw.vo.VisitStatistics;
import top.zhimingw.vo.VisitorStatistics;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author:zhimingw
 * @Version 1.0
 * @since 2021/6/7 19:49
 */
@Service("statisticsService")
public class StatisticsServiceImpl extends BaseService implements StatisticsService {
    @Override
    public boolean insert(Statistics statistics) {
        if(statisticsMapper.insert(statistics)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteById(Integer id) {
        if(statisticsMapper.deleteById(id)==1){
            return true;
        }
        return false;
    }

    @Override
    public Statistics queryById(Integer id) {
        return statisticsMapper.queryById(id);
    }

    @Override
    public List<Statistics> queryAll() {
        return statisticsMapper.queryAll();
    }

    @Override
    public List<Statistics> queryAll(Statistics statistics) {
        return statisticsMapper.queryAll(statistics);
    }


    @Override
    public void update(HttpServletRequest request, Integer aId) {
        // 获取请求的IP地址
        String ip = IPUtils.getIpAddress(request);
        redisService.incr(StatisticsKey.todayVisitor,ip+":"+aId);
    }

    @Override
    public Statistics findByIp(String ip, Integer aId) {
        return statisticsMapper.findByIp(ip,aId);
    }

    @Override
    public void addRequestCount(String ip, Integer aId) {
        statisticsMapper.addRequestCount(ip,aId);
    }

    @Override
    public List<Date> getRecentDates() {
        return statisticsMapper.getRecentDates();
    }

    @Override
    public List<Integer> getRecentRequests() {
        return statisticsMapper.getRecentRequests();
    }

    @Override
    public List<Integer> getRecentVisitors() {
        return statisticsMapper.getRecentVisitors();
    }

    /**
    * @Description: 获取访问数据
    * @Param: []
    * @return: top.zhimingw.vo.VisitStatistics
    * @Author: zhimingw
    * @Date: 2021/7/5
    */
    @Override
    public VisitStatistics getVisitStatistics() {
        VisitStatistics statistics = new VisitStatistics();
        // 设置日期
        List<Date> recentDates = getRecentDates();//获取访问日期 从最近开始排
        Collections.reverse(recentDates);//反转访问日期
        String[] dates = new String[10];
        for(int i=0;i<recentDates.size();i++){
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
            dates[i] = dateFormat.format(recentDates.get(i));
        }
        statistics.setDates(dates);
        // 设置访问量
        List<Integer> recentRequests = getRecentRequests();
        Collections.reverse(recentRequests);
        Integer[] requests = new Integer[10];
        for (int i = 0; i < recentRequests.size(); i++) {
            requests[i] = recentRequests.get(i);
        }
        statistics.setRequests(requests);
        // 设置访客量
        List<Integer> recentVisitors = getRecentVisitors();
        Collections.reverse(recentVisitors);
        Integer[] visitors = new Integer[10];
        for(int i=0;i<recentVisitors.size();i++){
            visitors[i] = recentVisitors.get(i);
        }
        statistics.setVisitors(visitors);
        return statistics;
    }

    /**
    * @Description: 获取所有访客数据
    * @Param: []
    * @return: java.util.List<top.zhimingw.vo.VisitorStatistics>
    * @Author: zhimingw
    * @Date: 2021/7/21
    */
    @Override
    public List<VisitorStatistics> getVisitorStatistics() {
        // 获取所有的访问信息
        List<Statistics> statistics = statisticsMapper.queryAll();//获得表中的所有数据
        List<VisitorStatistics> visitorStatistics = new ArrayList<>();//更新访问具体的信息
        statistics.forEach(temp -> {
            //判断访问的是文章还是主页
            if (temp.getArticleId() == -1) {
                visitorStatistics.add(new VisitorStatistics("主页", temp));
            } else {
                //获取访问的文章标题
                String title = articleMapper.getTitle(temp.getArticleId());
                if (!ObjectUtils.isEmpty(title)){//如果标题不为空
                    visitorStatistics.add(new VisitorStatistics(title, temp));
                }else {
                    visitorStatistics.add(new VisitorStatistics("其他", temp));
                }
            }
        });
        return visitorStatistics;

    }

    /**
    * @Description: 获取统计数量信息
    * @Param: []
    * @return: java.util.Map<java.lang.String,top.zhimingw.vo.StatisticsCount>
    * @Author: zhimingw
    * @Date: 2021/7/5
    */
    @Override
    public Map<String,StatisticsCount> getStatisticsCount() {
        Map<String, StatisticsCount> map = new HashMap<>();
        map.put("yesterday", new StatisticsCount(statisticsMapper.getRequestCount(1), statisticsMapper.getVisitorCount(1)));
        map.put("week", new StatisticsCount(statisticsMapper.getRequestCount(7), statisticsMapper.getVisitorCount(7)));
        map.put("month", new StatisticsCount(statisticsMapper.getRequestCount(30), statisticsMapper.getVisitorCount(30)));
        map.put("year", new StatisticsCount(statisticsMapper.getRequestCount(365), statisticsMapper.getVisitorCount(365)));
        map.put("total", new StatisticsCount(statisticsMapper.getRequestCount(-1), statisticsMapper.getVisitorCount(-1)));
        return map;
    }
}
